package org.jetlinks.pro.device.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.rdb.mapping.annotation.ColumnType;
import org.hswebframework.ezorm.rdb.mapping.annotation.DefaultValue;
import org.hswebframework.ezorm.rdb.mapping.annotation.EnumCodec;
import org.hswebframework.ezorm.rdb.mapping.annotation.JsonCodec;
import org.hswebframework.web.api.crud.entity.GenericEntity;
import org.jetlinks.core.message.MessageType;
import org.jetlinks.pro.device.enums.TaskState;
import org.jetlinks.pro.device.service.task.DeviceMessageTask;

import javax.persistence.Column;
import javax.persistence.Index;
import javax.persistence.Table;
import java.sql.JDBCType;
import java.util.Map;

@Getter
@Setter
@Table(name = "dev_message_task", indexes = {
    @Index(name = "idx_msg_task_devid", columnList = "device_id")
})
public class DeviceMessageTaskEntity extends GenericEntity<String> {

    @Schema(description = "发送指令的服务节点ID")
    @Column(nullable = false, length = 64, updatable = false)
    private String serverId;

    @Schema(description = "产品ID")
    @Column(nullable = false, length = 64, updatable = false)
    private String productId;

    @Schema(description = "设备ID")
    @Column(nullable = false, length = 64, updatable = false)
    private String deviceId;

    @Schema(description = "设备名称")
    @Column
    private String deviceName;

    @Schema(description = "消息ID")
    @Column(nullable = false, length = 64, updatable = false)
    private String messageId;

    @Column(nullable = false, updatable = false)
    @Schema(description = "消息类型")
    @EnumCodec
    @ColumnType(javaType = String.class)
    private MessageType messageType;

    @Schema(description = "下行消息内容")
    @JsonCodec
    @ColumnType(javaType = String.class, jdbcType = JDBCType.LONGVARCHAR)
    @Column(nullable = false, updatable = false)
    private Map<String, Object> downstream;

    @Schema(description = "上行回复内容")
    @JsonCodec
    @ColumnType(javaType = String.class, jdbcType = JDBCType.LONGVARCHAR)
    @Column
    private Map<String, Object> upstream;

    @Schema(description = "消息状态")
    @EnumCodec
    @ColumnType(javaType = String.class)
    @DefaultValue("wait")
    @Column(nullable = false)
    private TaskState state;

    @Schema(description = "错误码")
    @Column
    private String lastErrorCode;

    @Schema(description = "错误信息")
    @Column
    private String lastError;

    @Schema(description = "重试次数")
    @Column
    private Long retryTimes;

    @Schema(description = "最大重试次数")
    @Column
    private Long maxRetryTimes;

    @Schema(description = "发送时间")
    @Column(nullable = false)
    private Long sendTimestamp;

    @Schema(description = "回复时间")
    @Column
    private Long replyTimestamp;

    public static DeviceMessageTaskEntity of(DeviceMessageTask task) {

        return new DeviceMessageTaskEntity().copyFrom(task);
    }

    public DeviceMessageTask toTask(){

        return this.copyTo(DeviceMessageTask.class);
    }
}
